<?php
/**
 * 统一设置所有菜单的 sort 值，确保所有菜单都能正常显示
 */

$host = '113.44.136.17';
$port = 3306;
$database = 'qiandao';
$username = 'qiandao';
$password = 'NGbks3HWx5PhSxDX';

try {
    $pdo = new PDO("mysql:host={$host};port={$port};dbname={$database};charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "=== 统一设置菜单 SORT 值 ===\n\n";
    
    // 一级菜单的 sort 值设置
    $topMenuSort = [
        25 => 100,  // 打卡积分 - 业务核心，排第一
        9  => 90,   // 权限管理 - 系统管理，排第二
        1  => 80,   // 系统管理 - 系统配置，排第三
        12 => 70,   // 微信管理 - 微信功能，排第四
    ];
    
    echo "【1】设置一级菜单 SORT 值\n";
    foreach ($topMenuSort as $id => $sort) {
        $stmt = $pdo->prepare("UPDATE system_menu SET sort = ? WHERE id = ?");
        $stmt->execute([$sort, $id]);
        
        $stmt2 = $pdo->prepare("SELECT title FROM system_menu WHERE id = ?");
        $stmt2->execute([$id]);
        $title = $stmt2->fetchColumn();
        
        echo "  ✓ {$title} (ID:{$id}): SORT = {$sort}\n";
    }
    
    echo "\n【2】设置二级和三级菜单 SORT 值\n";
    echo "  策略：所有子菜单统一设置为 50（保持相对顺序）\n";
    
    $stmt = $pdo->prepare("UPDATE system_menu SET sort = 50 WHERE pid > 0");
    $stmt->execute();
    $count = $stmt->rowCount();
    echo "  ✓ 已更新 {$count} 个子菜单的 SORT 值为 50\n";
    
    echo "\n【3】验证最终菜单结构\n";
    $stmt = $pdo->query("
        SELECT id, pid, title, sort 
        FROM system_menu 
        WHERE status = 1 AND pid = 0 
        ORDER BY sort DESC, id ASC
    ");
    $topMenus = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    foreach ($topMenus as $menu) {
        echo "\n{$menu['title']} (ID:{$menu['id']}, SORT:{$menu['sort']})\n";
        
        // 查询子菜单
        $stmt2 = $pdo->prepare("
            SELECT id, title, sort 
            FROM system_menu 
            WHERE status = 1 AND pid = ? 
            ORDER BY sort DESC, id ASC
        ");
        $stmt2->execute([$menu['id']]);
        $subMenus = $stmt2->fetchAll(PDO::FETCH_ASSOC);
        
        foreach ($subMenus as $sub) {
            echo "  └─ {$sub['title']} (ID:{$sub['id']}, SORT:{$sub['sort']})\n";
            
            // 查询三级菜单
            $stmt3 = $pdo->prepare("
                SELECT id, title, sort 
                FROM system_menu 
                WHERE status = 1 AND pid = ? 
                ORDER BY sort DESC, id ASC
            ");
            $stmt3->execute([$sub['id']]);
            $subSubMenus = $stmt3->fetchAll(PDO::FETCH_ASSOC);
            
            foreach ($subSubMenus as $subsub) {
                echo "      └─ {$subsub['title']} (ID:{$subsub['id']}, SORT:{$subsub['sort']})\n";
            }
        }
    }
    
    echo "\n\n=== 完成！===\n";
    echo "菜单显示顺序（从上到下）：\n";
    echo "1. 打卡积分 (SORT=100)\n";
    echo "2. 权限管理 (SORT=90)\n";
    echo "3. 系统管理 (SORT=80)\n";
    echo "4. 微信管理 (SORT=70)\n\n";
    
    echo "请刷新后台页面（Ctrl+F5）查看效果\n";
    echo "所有菜单现在应该都能正常显示了！\n";
    
} catch (PDOException $e) {
    echo "数据库错误: " . $e->getMessage() . "\n";
}

